package com.app.common.db.Preferences;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by zhoulikai on 17/3/3.
 * 首选项存储
 */

public class PreferencesStorage implements IPreferencesStorage {

    private final String preferencesName = "preferences";
    private static PreferencesStorage preferencesStorage;
    private SharedPreferences sp;

    private PreferencesStorage(Context context) {
        sp = context.getSharedPreferences(preferencesName, Context.MODE_PRIVATE);
    }

    public static PreferencesStorage getInstance(Context context) {
        if (preferencesStorage == null) {
            preferencesStorage = new PreferencesStorage(context);
        }
        return preferencesStorage;
    }

    @Override
    public void setVale(String key, String value) {
        if (sp == null || TextUtils.isEmpty(key)) {
            return;
        }
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, value == null ? "" : value);
        editor.commit();

    }

    @Override
    public String getValue(String key, String defaultValue) {
        if (sp == null || TextUtils.isEmpty(key)) {
            return defaultValue;
        }
        return sp.getString(key, defaultValue);

    }

    @Override
    public void setVale(String key, boolean value) {
        if (sp == null || TextUtils.isEmpty(key)) {
            return;
        }
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(key, value);
        editor.commit();

    }

    @Override
    public boolean getValue(String key, boolean defaultValue) {
        if (sp == null || TextUtils.isEmpty(key)) {
            return defaultValue;
        }
        return sp.getBoolean(key, defaultValue);
    }

    @Override
    public void setVale(String key, int value) {
        if (sp == null || TextUtils.isEmpty(key)) {
            return;
        }
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(key, value);
        editor.commit();

    }

    @Override
    public int getValue(String key, int defaultValue) {
        if (sp == null || TextUtils.isEmpty(key)) {
            return defaultValue;
        }
        return sp.getInt(key, defaultValue);

    }

    @Override
    public void setVale(String key, float value) {
        if (sp == null || TextUtils.isEmpty(key)) {
            return;
        }
        SharedPreferences.Editor editor = sp.edit();
        editor.putFloat(key, value);
        editor.commit();

    }

    @Override
    public float getValue(String key, float defaultValue) {
        if (sp == null || TextUtils.isEmpty(key)) {
            return defaultValue;
        }
        return sp.getFloat(key, defaultValue);
    }

    @Override
    public void setVale(String key, long value) {
        if (sp == null || TextUtils.isEmpty(key)) {
            return;
        }
        SharedPreferences.Editor editor = sp.edit();
        editor.putLong(key, value);
        editor.commit();

    }

    @Override
    public long getValue(String key, long defaultValue) {
        if (sp == null || TextUtils.isEmpty(key)) {
            return defaultValue;
        }

        return sp.getLong(key, defaultValue);
    }

    @Override
    public void setVale(String key, Set<String> value) {
        if (sp == null || TextUtils.isEmpty(key)) {
            return;
        }
        SharedPreferences.Editor editor = sp.edit();
        editor.putStringSet(key, value == null ? new HashSet<String>() : value);
        editor.commit();

    }

    @Override
    public Set<String> getValue(String key, Set<String> defaultValue) {
        if (sp == null || TextUtils.isEmpty(key)) {
            return defaultValue;
        }

        return sp.getStringSet(key, defaultValue);
    }
}

